/*
 * Copyright (c) 2011, Gerold Roth
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of the gradecalculator.net nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package netgradecalculator.plugin.importer.hsrw.lsf.pdf;

import net.gradecalculator.plugin.importer.data.ImportedExaminationResult;
import net.gradecalculator.plugin.importer.data.ImportedExaminationResult.ResultState;

public final class LsfGradeParserUtil {
	
	private LsfGradeParserUtil() { }
	
	private static boolean isSemester(final String part)
	{
		return (part.equals("WS") || part.equals("SS"));
	}
	
	private static int getNameEndIndex(final String[] parts)
	{
		int i = 2;
		while (i < parts.length) {
			if (isSemester(parts[i])) {
				return i - 1;
			}
			i++;
		}
		
		return -1;
	}
	
	private static String getCombined(final String[] parts, final int start, final int end)
	{
		String result = "";
		for (int i = start; i <= end; i++) {
			result = result + " " + parts[i];
		}
		
		return result.trim();
	}

	public static ImportedExaminationResult createResult(final String courseOfStudies, final String line) {
		final ImportedExaminationResult result = new ImportedExaminationResult();
		result.setCourseOfStudies(courseOfStudies);
		final String[] parts = line.split(" ");
		
		final int nameEndIndex = getNameEndIndex(parts);
		final int gradeIndex = nameEndIndex + 3;
		final int ectsIndex = gradeIndex + 2;
		final int trialsIndex = ectsIndex + 1;
		
		result.setExaminationResultId(parts[0]);
		result.setCourseName(getCombined(parts, 1, nameEndIndex));
		result.setSemester(getCombined(parts, nameEndIndex + 1, nameEndIndex + 2));

		final String grade = parts[gradeIndex];
		result.setState(ResultState.PASSED);
		if (grade.equals("---")) {
			result.setGrade("");
		} else {
			result.setGrade(grade);
		}
		result.setEcts(Float.parseFloat(parts[ectsIndex].replace(',', '.')));
		
		if (parts.length > trialsIndex) {
			result.setTrials(Integer.parseInt(parts[trialsIndex]));
		}
		
		
		
		return result;
	}
}
